package com.library.gq.servlet;

import com.alibaba.fastjson.JSON;
import com.library.gq.dao.imlp.InsertDaoImpl;
import com.library.gq.entity.InsertEntity;
import com.library.gq.servce.impl.InsertServceImpl;
import com.library.gq.util.Page;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class InsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");          //设置代码格式
        InsertServceImpl isi = new InsertServceImpl();  //获取InsertServletImpl实体类
        InsertDaoImpl idi = new InsertDaoImpl();        //获取InsertServletImpl实体类
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        Integer currPage = Integer.parseInt(currentPage);

        String tp = req.getParameter("tp");     //获取操作码
        if(tp==null){
            tp="select";
        }
        /**
         * 如果操作码等于select进行查询操作
         */
        if (tp.equals("select")) {
            String name = req.getParameter("name_select");
            if (name == null || name.equals("")) {
                name = "全部";
            }
            Page<InsertEntity> page = isi.findByPage(name, currPage);
            req.setAttribute("page", page);
            req.getRequestDispatcher("gq/jsp/lostAndFound.jsp").forward(req, resp);
        }

        /**
         * 如果操作码等于insert进行插入操作
         */
        if (tp.equals("insert")) {
            //获取捐赠人姓名
            String name = req.getParameter("name");
            //获取书籍名
            String bookName = req.getParameter("bookName");
            //作者姓名
            String author = req.getParameter("author");
            //获取书籍类型
            String type = req.getParameter("type");
            //对书籍类型进行判断
            if (type.equals("网络小说")) {
                type = "0";
            } else if (type.equals("儿童益智")) {
                type = "1";
            } else if (type.equals("学习资料")) {
                type = "2";
            } else if (type.equals("经典名著")) {
                type = "3";
            }
            Integer counts = Integer.parseInt(type);
            //获取书籍数量
            Integer number = Integer.parseInt(req.getParameter("number"));
            //获取书籍价格
            Double price = Double.parseDouble(req.getParameter("price"));

            //获取书的位置
            String location =InsertServceImpl.getRandomString();


            //获取书的来源
            String source = req.getParameter("source");


            //获取书籍捐赠时间
            String date = req.getParameter("time");
            //获取插入donatebook成功后面的返回值
            Integer count = idi.insert(name, bookName, author, counts, number, price, date);
            //获取book表中的查询语句的返回值
            Integer book_select = idi.select(bookName);
            //判断book表中返回值是否为1,为1就为book的sum添加数量
            if (book_select == 1) {
                idi.update(number, bookName);
                //判断数据的返回值，如果添加成功.就转发到insert.jsp页面
                if (count == 1) {
                    req.getRequestDispatcher("gq/jsp/insert.jsp").forward(req, resp);
                }
                //判断book表中返回值是否为1,不为1就添加到书籍表
            } else if (book_select != 1) {
                idi.insert_book(bookName, counts, author, number, price, location, source, date);
                req.getRequestDispatcher("gq/jsp/insert.jsp").forward(req, resp);
            }

        }
        /**
         * 删除
         */
        if (tp.equals("delete")) {
            Integer ID = Integer.parseInt(req.getParameter("workingID"));
            Integer del=idi.delete(ID);
            System.out.println(del);
            if (del==1){
                req.getRequestDispatcher("gq/jsp/lostAndDound.jsp");
            }
            boolean isDel = isi.findDel(Integer.parseInt("workingID"));
            String s = JSON.toJSONString(isDel);
            PrintWriter printWriter = resp.getWriter();
            printWriter.print(s);
            printWriter.flush();
            printWriter.close();
        }
    }
}